{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Aggregator examples\n",
    "Shows how dfs aggregator functions work"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import mikeio\n",
    "from mikeio import aggregator\n",
    "dfs0 = mikeio.dfs0.Dfs0()\n",
    "dfs1 = mikeio.dfs1.dfs1()\n",
    "dfs2 = mikeio.dfs2.dfs2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Input data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 20, 5)\n",
      "['Item 1']\n",
      "0.44341139290481807\n",
      "0.5089342057704925\n",
      "0.9117105007171631\n",
      "0.4953838095674291\n",
      "0.2997477275240475\n"
     ]
    }
   ],
   "source": [
    "dfs2file = r\"..\\tests\\testdata\\eq.dfs2\"\n",
    "data2, tt2, names2 = dfs2.read(dfs2file)\n",
    "print(data2[0].shape)\n",
    "print(names2)\n",
    "print(np.nanmean(data2[0][:,0,0]))\n",
    "print(np.nanmean(data2[0][0,:,0]))\n",
    "print(np.nanmax(data2[0][:,0,0]))\n",
    "#\n",
    "s1, s2, s3 = data2[0].shape\n",
    "vec=np.reshape(data2[0][:,:,0],s1*s2,1)\n",
    "print(np.nanmean(vec))\n",
    "print(np.nanstd(vec))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1, s2, s3 = data2[0].shape\n",
    "vec=np.reshape(data2[0][:,:,0],s1*s2,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1\n",
    "make y-average of dfs2 (keeping x-axis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 5)\n",
      "['Item 1']\n",
      "0.4434114098548889\n"
     ]
    }
   ],
   "source": [
    "dfs1file1 = r\"..\\tests\\testdata\\eq_ax_x.dfs1\"\n",
    "aggregator.dfs2todfs1(dfs2file, dfs1file1)\n",
    "data1, tt1, names1 = dfs1.read(dfs1file1)\n",
    "print(data1[0].shape)\n",
    "print(names1)\n",
    "print(data1[0][0,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2: other axis\n",
    "Take average instead over x-axis "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 5)\n",
      "0.5089341998100281\n"
     ]
    }
   ],
   "source": [
    "dfs1file2 = r\"..\\tests\\testdata\\eq_ax_y.dfs1\"\n",
    "aggregator.dfs2todfs1(dfs2file, dfs1file2, ax=1)\n",
    "data1, tt1, names1 = dfs1.read(dfs1file2)\n",
    "print(data1[0].shape)\n",
    "print(data1[0][0,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 3: max\n",
    "Take max instead over y-axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 5)\n",
      "0.9117105007171631\n"
     ]
    }
   ],
   "source": [
    "dfs1file3 = r\"..\\tests\\testdata\\eq_ax_x_max.dfs1\"\n",
    "aggregator.dfs2todfs1(dfs2file, dfs1file3, ax=0, func=np.nanmax)\n",
    "data1, tt1, names1 = dfs1.read(dfs1file3)\n",
    "print(data1[0].shape)\n",
    "print(data1[0][0,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4: aggregate to dfs0\n",
    "Calc mean of each time step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "['Item 1']\n",
      "0.4953838288784027\n"
     ]
    }
   ],
   "source": [
    "dfs0fileA = r\"..\\tests\\testdata\\eq_mean.dfs0\"\n",
    "aggregator.dfstodfs0(dfs2file, dfs0fileA)\n",
    "data0, tt0, names0 = dfs0.read(dfs0fileA)\n",
    "print(data0[0].shape)\n",
    "print(names0)\n",
    "print(data0[0][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 5: calc standard deviation pr step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2997477352619171\n"
     ]
    }
   ],
   "source": [
    "dfs0fileB = r\"..\\tests\\testdata\\eq_std.dfs0\"\n",
    "aggregator.dfstodfs0(dfs2file, dfs0fileB, np.nanstd)\n",
    "data0, tt0, names0 = dfs0.read(dfs0fileB)\n",
    "print(data0[0][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clean up"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.remove(dfs1file1)\n",
    "os.remove(dfs1file2)\n",
    "os.remove(dfs1file3)\n",
    "os.remove(dfs0fileA)\n",
    "os.remove(dfs0fileB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}